<template>
  <div class="searchBox">
    <div class="row">
      <div class="left">
        <el-icon><Search /></el-icon>
        <span>筛选搜索</span>
      </div>
      <div class="right">
        <el-button @click="params.keyword = ''">重置</el-button>
        <el-button type="primary" @click="searchUser">查询搜索</el-button>
      </div>
    </div>
    <div class="row">
      <span>账号/姓名:</span>
      <el-input
        placeholder="请输入姓名、账号搜索"
        v-model="params.keyword"
      ></el-input>
    </div>
  </div>
  <div class="addArea">
    <el-button
      type="primary"
      @click="showDialog = true"
      v-permission="'addUser'"
      >添加</el-button
    >
  </div>
  <el-table :data="listVal" border cell-class-name="alignC" table-layout="auto">
    <el-table-column label="编号" prop="id" />
    <el-table-column label="账号" prop="username" />
    <el-table-column label="姓名" prop="nickName" />
    <el-table-column label="邮箱" prop="email" />
    <el-table-column label="添加时间" prop="createTime">
      <template #default="{ row }">
        {{ formateDate(row.createTime) }}
      </template>
    </el-table-column>
    <el-table-column label="最后登录" prop="loginTime">
      <template #default="{ row }">
        {{ formateDate(row.loginTime) }}
      </template>
    </el-table-column>
    <el-table-column label="是否启用">
      <template #default="{ row }">
        <el-switch
          v-model="row.status"
          :active-value="1"
          :inactive-value="0"
          @click="changeStatus(row)"
        ></el-switch>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button
          text
          type="primary"
          @click="allocRole(row.id)"
          v-permission="'allocRole'"
          >分配角色</el-button
        >
        <el-button
          text
          type="primary"
          @click="edit(row.id)"
          v-permission="'editUser'"
          >编辑</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页器 -->
  <ElConfigProvider :locale="zhCn">
    <div class="page">
      <el-pagination
        v-model:current-page="params.pageNum"
        v-model:page-size="params.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="getUserList"
        @current-change="getUserList"
      />
    </div>
  </ElConfigProvider>
  <EditUser :show="showDialog" @hide="switchDialog" :id="editId"></EditUser>
  <AllocRole :show="showRole" :id="roleId" @hide="closeAllocRole"></AllocRole>
</template>
<script setup lang="ts">
import { userListApi, changeUserStatueApi } from "./api";
import { ref, reactive } from "vue";
import { Search } from "@element-plus/icons-vue";
import { ElConfigProvider, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { formateDate } from "@/utils";
import EditUser from "./editUser.vue";
import AllocRole from "./allocRole.vue";

const params = reactive<IApiParams>({
  keyword: "",
  pageNum: 1,
  pageSize: 10,
});
const total = ref(0);
const showDialog = ref(false);
const listVal = ref<List[]>([]);
const editId = ref(0);
const showRole = ref(false);
const roleId = ref(0);

const switchDialog = (reLoad?: Boolean) => {
  showDialog.value = false;
  editId.value = 0;
  if (reLoad) getUserList();
};
const searchUser = () => {
  getUserList();
};

const edit = (id: number) => {
  showDialog.value = true;
  editId.value = id;
};
const allocRole = (id: number) => {
  showRole.value = true;
  roleId.value = id;
};
const closeAllocRole = (reLoad?: Boolean) => {
  showRole.value = false;
  roleId.value = 0;
  if (reLoad) getUserList();
};

const changeStatus = (row: List) => {
  changeUserStatueApi(row.id as number, row.status)
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success("修改成功");
      } else {
        //业务状态不为200
        row.status = row.status === 1 ? 1 : 0;
      }
    })
    .catch(() => {
      //请求状态不为200
      row.status = row.status === 1 ? 1 : 0;
    });
};

const getUserList = () => {
  userListApi(params).then((res) => {
    if (res.code === 200) {
      listVal.value = res.data.list;
      total.value = res.data.total;
    }
  });
};
getUserList();
</script>
<style lang="less" scoped>
:deep(.alignC) {
  .cell {
    text-align: center;
  }
}
.page {
  display: flex;
  flex-direction: row-reverse;
}
:deep(.align-c) {
  .cell {
    text-align: center;
  }
}
.searchBox {
  box-shadow: 0 0 4px 2px #ccc;
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 30px;
  .row {
    padding: 10px 0;
    display: flex;
    &:first-child {
      justify-content: space-between;
    }
    &:last-child {
      width: 400px;
      span {
        width: 100px;
      }
    }
  }
}

.page {
  display: flex;
  flex-direction: row-reverse;
  :deep(.el-pagination) {
    margin-top: 20px;
  }
}
</style>
